<?php

function _getSizeName($match) {
    $arrSize = array(
        '8pt',
        '10pt',
        '12pt',
        '14pt',
        '18pt',
        '24pt',
        '36pt'
    );
    return '<span style="font-size:' . (isset($arrSize[$match[1] - 1]) ? $arrSize[$match[1] - 1] : 12) . ';">';
}

function _getMedia($match) {
    $w = $match[1];
    $h = $match[2];
    $play = $match[3];
    $url = $match[4];
    if (!$w) $w = 550;
    if (!$h) $h = 400;
    return '<embed type="application/x-mplayer2" src="' . $url . '" enablecontextmenu="false" autostart="' . ($play == '1' ? 'true' : 'false') . '" width="' . $w . '" height="' . $h . '"/>';
}

function _getTable($match) {
    $w = $match[1];
    $b = $match[2];
    $str = '<table';
    if ($w) $str.= ' width="' . $w . '"';
    if ($b) $str.= ' bgcolor="' . $b . '"';
    return $str . '>';
}

function _getTD($match) {
    $col = $match[1];
    $row = $match[2];
    $w = $match[3];
    return '<td' . ($col > 1 ? ' colspan="' . $col . '"' : '') . ($row > 1 ? ' rowspan="' . $row . '"' : '') . ($w ? ' width="' . $w . '"' : '') . '>';
}

function _getTR($match) {
    if (isset($match[1])) return '<tr' . ($match[1] ? ' bgcolor="' . $match[1] . '"' : '') . '>';
}

function _getUL($match) {
    //ol ul
    if (isset($match[1]) && $match[1] == '1') {
        $str = '<ol';
    }
    else {
        $str = '<ul';
    }
    if (isset($match[1]) && $match[1]) $str.= ' type="' . $match[1] . '"';
    return $str . '>';
}

function _getULClose($match) {
    //ol ul
    if (isset($match[1]) && $match[1] == '1') {
        $str = '</ol>';
    }
    else {
        $str = '</ul>';
    }
    //if(isset($match[1])&&$match[1])$str.=' type="'.$match[1].'"';
    return $str;
}

function _getIndent($match) {
    if ($match[1]) return '<blockquote>' . $match[1] . '</blockquote>';
}
/**
 *  UBB 到 html 的互相转换
 */

function ubb2html($sUBB,$exchange_em=TRUE) {
    $sHtml = $sUBB;
    $sHtml = str_replace("[img][/img]", '', $sHtml);
    ////去掉一些不是规格地址的图片
    
    //$sHtml=preg_replace("/^(\[img\]\s*http:\/\/[\w\.\/-]+?\.jpg\[/img\])/",'',$sHtml);
    $sHtml = preg_replace("/</", '&lt;', $sHtml);
    $sHtml = preg_replace("/>/", '&gt;', $sHtml);
    $sHtml = preg_replace("/\r?\n/", '<br/>', $sHtml);
    $sHtml = preg_replace("/\[(\/?)(b|u|i|s|sup|sub)\]/i", '<$1$2>', $sHtml);
    $sHtml = preg_replace("/\[color\s*=\s*([^\]]+?)\]/i", '<span style="color:$1;">', $sHtml);
    $sHtml = preg_replace_callback("/\[size\s*=\s*([\d\.]+?)(p[tx])?\]/i", '_getSizeName', $sHtml);
    $sHtml = preg_replace("/\[font\s*=\s*([^\]]+?)\]/i", '<span style="font-family:$1;">', $sHtml);
    $sHtml = preg_replace("/\[back\s*=\s*([^\]]+?)\]/i", '<span style="background-color:$1;">', $sHtml);
    $sHtml = preg_replace("/\[\/(color|size|font|back)\]/i", '</span>', $sHtml);
    //for ($i = 0;$i < 3;$i++) {
    //    //$sHtml = preg_replace("/\[align\s*=\s*([^\]]+?)\](((?!\[align(?:\s+[^\]]+)?\])[\s\S])*?)\[\/align\]/", '<p align="$1">$2</p>', $sHtml);
    //    //var_dump($sHtml);
    //    //$sHtml = preg_replace("/\[align\s*=\s*[\"\']?(\w+)[\"\']?\s*\](.*)\[\/align\]/", '<p align="$1">$2</p>', $sHtml);
    //}
    
    $sHtml=preg_replace("/\[align\s*=\s*[\"\']?(\w+)[\"\']?\s*\]/",'<p align="$1">$2',$sHtml);
    $sHtml=preg_replace("/\[\/align\]/",'</p>',$sHtml);
    
    $sHtml = preg_replace("/\[img\]\s*([\s\S]+?)\s*\[\/img\]/i", '<img src="$1" />', $sHtml);
    $sHtml = preg_replace("/\[p[\s\S]*?\]/i", '<p>', $sHtml);
    $sHtml = preg_replace("/\[\/p\]/i", '</p>', $sHtml);
    $sHtml = preg_replace("/\[div\]/i", '<div>', $sHtml);
    $sHtml = preg_replace("/\[\/div\]/i", '</div>', $sHtml);
    $sHtml = preg_replace("/\[[\/]?float[\w=]*?\]/i", '', $sHtml);
    $sHtml = preg_replace("/\[br\]/i", '<br/>', $sHtml);
    $sHtml = preg_replace("/\[ns\]/i", '&nbsp;', $sHtml);
    $sHtml = preg_replace("/\[img\s*=\s*(\d+),(\d+)\s*\]\s*([\s\S]+?)\s*\[\/img\]/i", '<img src="$3" width="$1" height="$2" />', $sHtml);
    $sHtml = preg_replace("/\[url\]\s*([\s\S]+?)\s*\[\/url\]/i", '<a target=\'_blank\' href="$1">$1</a>', $sHtml);
    $sHtml = preg_replace("/\[url\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/url\]/i", '<a target=\'_blank\'  href="$1">$2</a>', $sHtml);
    $sHtml = preg_replace("/\[url\s*=\s*([^\]\s]+?)\s*\]/i", '', $sHtml);
    $sHtml = preg_replace("/\[\/url\]/i", '', $sHtml);
    //$sHtml=preg_replace("/\[email\]\s*([\s\S]+?)\s*\[\/email\]/i",'<a href="mailto:$1">$1</a>',$sHtml);
    
    //$sHtml=preg_replace("/\[email\s*=\s*([^\]\s]+?)\s*\]\s*([\s\S]+?)\s*\[\/email\]/i",'<a href="mailto:$1">$2</a>',$sHtml);
    
    //$sHtml=preg_replace("/\[quote\]([\s\S]*?)\[\/quote\]/i",'<blockquote>$1</blockquote>',$sHtml);
    $sHtml = preg_replace("/\[(\/)?quote\]/i", '<$1blockquote>', $sHtml);
    $sHtml = preg_replace("/\[(\/)?indent\]/i", '<$1blockquote>', $sHtml);
    //$sHtml=preg_replace("/\[indent\]/i",'<blockquote>',$sHtml);
    
    //$sHtml=preg_replace_callback("/\[indent\]([\s\S]*?)\[\/indent\]/i",'_getIndent',$sHtml);
    
    //$sHtml=preg_replace_callback("/\[indent\]([\s\S]*?)\[\/indent\]/i",'_getIndent',$sHtml);
    
    //$sHtml=preg_replace_callback("/\[indent\]([\s\S]*?)\[\/indent\]/i",'_getIndent',$sHtml);
    
    //$sHtml=preg_replace("/\[code\]([\s\S]*?)\[\/code\]/i",'<code>$1</code>',$sHtml);
    
    //function getFlash($match)
    
    //{
    
    //        $w=$match[1];$h=$match[2];$url=$match[3];
    
    //        if(!$w)$w=550;if(!$h)$h=400;
    
    //        return '<embed type="application/x-shockwave-flash" src="'.$url.'" wmode="opaque" quality="high" bgcolor="#ffffff" menu="false" play="true" loop="true" width="'.$w.'" height="'.$h.'"/>';
    
    //}
    
    //$sHtml=preg_replace_callback("/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]([\s\S]+?)\[\/flash\]/i",'getFlash',$sHtml);
    $sHtml = preg_replace_callback("/\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?)?\]([\s\S]+?)\[\/media\]/i", '_getMedia', $sHtml);
    $sHtml = preg_replace_callback("/\[table(?:\s*=\s*(\d{1,4}%?)\s*(?:,\s*([^\]]+)\s*)?)?]/i", '_getTable', $sHtml);
    $sHtml = preg_replace_callback("/\[tr(?:\s*=(\s*[^\]]+))?\]/i", '_getTR', $sHtml);
    $sHtml = preg_replace_callback("/\[td(?:\s*=\s*(\d{1,2})\s*,\s*(\d{1,2})\s*(?:,\s*(\d{1,4}%?))?)?\]/i", '_getTD', $sHtml);
    $sHtml = preg_replace("/\[\/(table|tr|td)\]/i", '</$1>', $sHtml);
    $sHtml = preg_replace("/\[\*\]([^\[]+)/i", '<li>$1</li>', $sHtml);
    $sHtml = preg_replace_callback("/\[list(?:\s*=\s*([^\]]+)\s*)?\]/i", '_getUL', $sHtml);
    $sHtml = preg_replace_callback("/\[\/list(?:\s*=\s*([^\]]+)\s*)?\]/i", '_getULClose', $sHtml);
    //去除剩余未过滤的标签
    $sHtml = preg_replace("/\[\/?p\]/i", "", $sHtml);
    //$sHtml=preg_replace("/\[\/list=?(\d)?\]/i",'</ul>',$sHtml);
    
    //表情切换
    $exchange_em&&($sHtml=F::emExchange($sHtml));
    return $sHtml;
}
/**
 *  ubb2plain
 */

function ubb2plain($ubb) {
    $txt = preg_replace("/\[(img|IMG)[\d\w=,]*\][\s\S]+?\[\/(img|IMG)\]/i", '', $ubb);
    $txt = preg_replace("/\[\/?[\s\S]+?\]/i", '', $txt);
    return trim($txt);
}
/**
 *  html2ubb
 */

function html2ubb($html) {
    $allowed_tag = "<p><img><br>";
    $txt = strip_tags($html, $allowed_tag);
    $txt = preg_replace("/<p>([\s\S]+)<\/p>/i", '[p]$1[/p]', $txt); //<p> q去掉最外层的p
    $txt = preg_replace("/<p>([\s\S]+?)<\/p>/i", '[p]$1[/p]', $txt); //<p>
    $txt = preg_replace("/<img\s+src=[\"\']?([^\"\']+)[\"\']?[\s\S]*?\/?>/i", '[img]$1[/img]', $txt);
    $txt = preg_replace("/<br\/>/i", '[br]', $txt); //br
    return $txt;
}
